<?php

/**
 * Description of db
 *
 * @author merabi
 */
class db_old extends system {

    private $dblink = '';
    private $conf;
    private $resourceStorage = [];
    private $currentResourceName = '__current__';
    private $defaults = ['fetchType' => 'assoc'];

    public function __construct($conf){
        parent::__construct();
        $this->conf = is_array($conf) ? $conf : (array) $conf;
        return $this;
    }

    /**
     * @return db
     */
    public function connect(){
        $this->dblink = mysql_connect($this->conf['host'], $this->conf['user'], $this->conf['pass']) or $this->error();
        return $this;
    }

    /**
     * @return db
     * @param string $database_name
     */
    public function selectdb($database_name){
        mysql_select_db($database_name, $this->dblink) or $this->error();
        return $this;
    }

    /**
     * 
     * @param string $query
     * @param string $resourcename
     * 
     * @return db
     */
    public function query($query, $resourcename = NULL){
        $this->currentResourceName = !$resourcename ? $this->currentResourceName : (string) $resourcename;
        $this->resourceStorage[$this->currentResourceName] = mysql_query($query, $this->dblink) or $this->error();
        return $this;
    }

    public function fetch($resourcename = NULL, $type = NULL){
        $this->currentResourceName = !$resourcename ? $this->currentResourceName : (string) $resourcename;
        $type = !$type ? $this->defaults['fetchType'] : (string) $type;
        $f = 'mysql_fetch_' . $type;
        $result = $f($this->resourceStorage[$this->currentResourceName]);
        return $result;
    }

    public function unsetResource($resourcename = NULL){
        unset($this->resourceStorage[$resourcename === NULL ? $this->currentResourceName : $resourcename]);
        return $this;
    }

    public function conf($confName){
        return $this->xArrayPath($this->conf, $confName);
    }

    public function fetchAll($resourcename = NULL, $type = NULL){
        $data = array();
        while(($row = $this->fetch($resourcename, $type)) != false){
            foreach($row as $i => $v){
                if(is_numeric($v))
                    $row[$i] = (float) $v;
            }
            $data[] = $row;
        }
        return $data;
    }

    public function lastInsertID(){
        return mysql_insert_id();
    }

    public function escapeSqlString($string){
        $string = strtr($string, [chr("0") => "", chr("1") => "", chr("2") => "", chr("3") => "", chr("4") => "", chr("5") => "", chr("6") => "", chr("7") => "", chr("8") => "", chr("9") => "", chr("10") => "", chr("11") => "", chr("12") => "", chr("13") => "", chr("14") => "", chr("15") => "",
            chr("16") => "", chr("17") => "", chr("18") => "", chr("19") => "", chr("20") => "", chr("21") => "", chr("22") => "", chr("23") => "", chr("24") => "", chr("25") => "", chr("26") => "", chr("27") => "", chr("28") => "", chr("29") => "", chr("30") => "", chr("31") => ""]);
        $string = str_replace(["\\r", "\\n", "\r\n", "\n", "\r"], " ", $string);
        $string = preg_replace("/(\s){2,}/", '$1', $string); //Do not allow repeated whitespace
        $string = preg_replace('{( ?.)\1{4,}}', '$1$1$1', $string); //Do not allow more than 3 identical characters separated by any whitespace
        $string = strip_tags($string); // Strip all other HTML tags 
        $string = htmlspecialchars($string, ENT_QUOTES, "UTF-8");
        $string = addslashes($string);
        return mysql_real_escape_string($string, $this->dblink);
    }

    public function esc($string){
        return mysql_real_escape_string($string, $this->dblink);
    }

    private function error(){
        die(mysql_error());
    }

    public function fieldInfoAll($resourcename = false){
        $this->currentResourceName = !$resourcename ? $this->currentResourceName : (string) $resourcename;
        $i = 0;
        while($i < mysql_num_fields($this->resourceStorage[$this->currentResourceName])){
            $meta = mysql_fetch_field($this->resourceStorage[$this->currentResourceName], $i);
            $fieldinfo[$meta->name] = [
                "blob" => $meta->blob,
                "max_length" => $meta->max_length,
                "multiple_key" => $meta->multiple_key,
                "name" => $meta->name,
                "not_null" => $meta->not_null,
                "numeric" => $meta->numeric,
                "primary_key" => $meta->primary_key,
                "table" => $meta->table,
                "type" => $meta->type,
                //"default" => $meta->def,
                "unique_key" => $meta->unique_key,
                "unsigned" => $meta->unsigned,
                "zerofill" => $meta->zerofill
            ];
            $i++;
        }
        return $fieldinfo;
    }
    
    public function fullFields($table_name){
        $table_name=$this->esc($table_name);
        return $this->query("show fields from {$table_name}")->fetchAll();
    }

    /**
     * affected rows - insert, update, replace, delete
     */
    public function affectedRows(){
        $return = mysql_affected_rows($this->dblink);
        return $return;
    }

    /**
     * number of rows SELECT or SHOW
     * 
     */
    public function numRows($resourcename = false){
        $this->currentResourceName = !$resourcename ? $this->currentResourceName : (string) $resourcename;
        $return = mysql_num_rows($this->currentResourceName);
        return $return;
    }

}

?>
